package com.surprise.ods.aliyun;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import com.surprise.ods.OdsConfig;

/**
 * OSSService.java
 * @author nick
 * @since 2015年8月17日
 */
@Service
public class OSSService {
	@Autowired
	private OdsConfig config;
	
	public String put(String key, String name, InputStream io, long length, String contentType) {
		OSSClient client = new OSSClient(config.getEndpoint(), config.getAk(),
				config.getSk());
		ObjectMetadata meta = new ObjectMetadata();
		meta.setContentLength(length);
		meta.setContentType(contentType);
		String path = key + "/" + name;
		PutObjectResult result = client.putObject(config.getBucket(), path, io,
				meta);
		System.out.println(meta.getETag());
		System.out.println(result.getETag());
		return config.getBaseUrl() + "/" + path;
	}
	
	public String put(String key, String name, InputStream io, long length) {
		return put(key, name, io, length, "image/jpeg");
	}
	
	public String put(String key, String name, byte[] bs, String contentType) {
		InputStream io = new ByteArrayInputStream(bs);
		int length = bs.length;
		return put(key, name, io, length, contentType);
	}
	
	public String put(String key, String name, byte[] bs) {
		return put(key, name, bs, "image/jpeg");
	}
}
